<?php

include_once(dirname(__FILE__).'/../services.database.php');

class DBProduct extends DataService
{

    public function getInstance()
    {
        return(new DBProduct());
    }

    /*-----------------------------------------------------------------------------------------------------
      ----------------------------------- Administración de Productos -------------------------------------
      -----------------------------------------------------------------------------------------------------*/

    public function insertProduct($id_tag,$esl_tag_alias,$esl_tag_barcode,$esl_tag_id_producto)
    {		
		$product_info = array(
            //'id_tag' =>$id_tag,
            'esl_tag_barcode' => $esl_tag_barcode,
            'esl_tag_alias' => $esl_tag_alias
        );

		$this->getLogger()->debug( print_r( $product_info,true ) );

        return $this->executeInsert('esl_sistema_tag',$product_info);
    }

    public function updateProduct($id_tag,$esl_tag_alias,$esl_tag_barcode,$esl_tag_id_producto)
    {
        $product_info = array(
            'id_tag' 	      => $id_tag,
            'esl_tag_alias'   => $esl_tag_alias,
			'esl_tag_barcode' => $esl_tag_barcode,
            'esl_tag_id_producto' => $esl_tag_id_producto
        );
		
		if($esl_tag_id_producto == '')
		{
			$product_info['esl_tag_id_producto'] = null;		
		}
        
        $this->getLogger()->debug( print_r( $product_info,true ) );
        
        $this->executeUpdate('esl_sistema_tag',$product_info,"id_tag = '${id_tag}'");
        
        //$sql = "UPDATE esl_sistema_tags SET esl_tag_alias='${esl_tag_alias}' WHERE id_tag='${id_tag}'";
		
		//$this->SafeExec($sql);
        
        return $id_tag;
    }

    public function deleteProduct($id_tag)
    {
        $sql ="DELETE
                FROM esl_sistema_tag
                WHERE id_tag = '${id_tag}'
              ";
              
        $this->SafeExec($sql);
        
        return true;
    }
    
    public function getProduct($id_prd)
    {
        /*$sql ="SELECT *
                 FROM rcd_product_list
                WHERE id_prd = %i
              ";
              
        return $this->SafeGetRow($sql);*/
    }

	public function linkProduct($id_tag,$esl_tag_id_producto)
	{
        $product_info = array(
            'id_tag' =>$id_tag,
            'esl_tag_id_producto' => $esl_tag_id_producto
        );
        
        $this->executeUpdate('esl_sistema_tag',$product_info,"id_tag = '${id_tag}'");
        
        return $id_tag;
	}
    
    /*-----------------------------------------------------------------------------------------------------
      ---------------------------------------------- Otros ------------------------------------------------
      -----------------------------------------------------------------------------------------------------*/
    
	public function listTags($params)
    {
        $sql = "SELECT 	*
                FROM 	esl_sistema_tag_list
                WHERE 	1=1";

        return $this->SafeGetAll($sql);
    }
	
    public function listProducs($params)
    {
       $sql = "   SELECT *
               	    FROM esl_sistema_producto 
               LEFT JOIN esl_sistema_tipo_unidad ON esl_producto_id_tipo_unidad = id_tipo_unidad
               WHERE 	1=1";

        return $this->SafeGetAll($sql);
    }


	public function listTagData()
	{
		$sql = "	SELECT *
					  FROM esl_sistema_tag_data";
	
        return $this->SafeGetAll($sql);
	}

}

?>
